package com.example.yiwei.crm.view.activity.customerActivity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.view.ContextThemeWrapper;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.TimePickerView;
import com.example.yiwei.crm.R;
import com.example.yiwei.crm.base.BaseActivity;
import com.example.yiwei.crm.base.baseClass;
import com.example.yiwei.crm.dao.clueDao;
import com.example.yiwei.crm.dao.managerDao;
import com.example.yiwei.crm.dao_impl.clueDaoImpl;
import com.example.yiwei.crm.dao_impl.managerDaoImpl;
import com.example.yiwei.crm.data.GetAreData;
import com.example.yiwei.crm.data.GetClue;
import com.example.yiwei.crm.data.GetClueData;
import com.example.yiwei.crm.data.GetContact;
import com.example.yiwei.crm.data.GetCustumer;
import com.example.yiwei.crm.data.GetPeople;
import com.example.yiwei.crm.entity.BZFLEntity;
import com.example.yiwei.crm.entity.ClueMeesageEntity;
import com.example.yiwei.crm.entity.ContactMessageEntity;
import com.example.yiwei.crm.entity.CustomerMessageEntity;
import com.example.yiwei.crm.entity.PeopleEntity;
import com.example.yiwei.crm.entity.managerEntity;
import com.example.yiwei.crm.json.CoorCustomerJson;
import com.example.yiwei.crm.util.NetWorkUtils;
import com.example.yiwei.crm.util.SC;
import com.example.yiwei.crm.util.ZhengZhe;
import com.example.yiwei.crm.view.activity.areActivity.ProvinceActivity;
import com.example.yiwei.crm.view.activity.clueActivity.ClueDetailedMessageActivity;
import com.example.yiwei.crm.view.activity.coordinationActivity.CoorCustomerActivity;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

public class CustomerAddActivity extends BaseActivity {
    private Spinner spinner_disease_category,spinner_manager;
    private EditText edit_name,edit_age,edit_contact_name,edit_mobile_phone,edit_id_card,edit_adress;
    private Button btn_show,btn_show1;
    private TextView txt_birth_time;
    private RelativeLayout relativeLayout;
    public static CustomerAddActivity a;
    private String pname,cname,aname,pid,cid,aid;
    private RadioGroup rg_sex;
    private RadioButton rb_men,rb_women;
    private ArrayAdapter adapter1,adapter2;
    private TextView txt_next_time,txt_adress;
    private TimePickerView mpv;
    private TimePickerView mpv1;
    private ImageView img_back,img_commit;
    private String birth_time;
    String next_time;
    String sresult;
    Date date2;
    Date date1;
    List<managerEntity> managerList = new ArrayList<>();
    ClueMeesageEntity clue;
    private List<String> name_list=new ArrayList<>();
    String managerName,result,disease_category;
    List<Map<String,String>> customerSpinner;
    int manager_id,sex,disease_category_id;
    List<String> client_list;
    List<CustomerMessageEntity> customerlist;
    CustomerMessageEntity customer;
    CustomerMessageEntity customers;
    ContactMessageEntity contact;
    String manager_name_show,disease_category_show;
    private List<CustomerMessageEntity> list;
    SharedPreferences sp;
    List<BZFLEntity> disease_category_list;
    int customer_id;
    Button showbutton;
    int c=1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_customer_add);
        a=this;
        sp = getSharedPreferences("login", Context.MODE_PRIVATE);
        txt_adress = (TextView) findViewById(R.id.customer_add_base_adress);
        showbutton = (Button) findViewById(R.id.customer_add_base_adress_show);
        edit_id_card= (EditText) findViewById(R.id.customer_add_edit_idcard);
        edit_adress = (EditText) findViewById(R.id.customer_add_edit_adress);
//        edit_bd = (EditText) findViewById(R.id.customer_add_edit_bd);
//        spinner_disease_category= (Spinner) findViewById(R.id.customer_add_spinner_disease_category);
        spinner_manager= (Spinner) findViewById(R.id.customer_add_spinner_manager);
        edit_age= (EditText) findViewById(R.id.customer_add_edit_age);
        edit_name= (EditText) findViewById(R.id.customer_add_edit_name);
        edit_contact_name= (EditText) findViewById(R.id.customer_add_edit_contact_name);
        edit_mobile_phone= (EditText) findViewById(R.id.customer_add_edit_mobile_phone);
        btn_show= (Button) findViewById(R.id.customer_add_btn_show);
        btn_show1= (Button) findViewById(R.id.customer_add_btn_show1);
        txt_birth_time= (TextView) findViewById(R.id.customer_add_birth_time);
        rg_sex= (RadioGroup) findViewById(R.id.customer_add_rg_sex);
        rb_men= (RadioButton) findViewById(R.id.customer_add_rb_men);
        rb_women= (RadioButton) findViewById(R.id.customer_add_rb_women);
        txt_next_time= (TextView) findViewById(R.id.customer_add_next_time);
        img_back= (ImageView) findViewById(R.id.customer_add_img_back);
        img_commit= (ImageView) findViewById(R.id.customer_add_img_commit);
        relativeLayout= (RelativeLayout) findViewById(R.id.customer_add_relative_manager);
        if ("saomiao".equals(getIntent().getStringExtra("ly"))){
            setSFMessage();
        }
        if ("kehuchi".equals(getIntent().getStringExtra("kehuchiTag"))){
            relativeLayout.setVisibility(View.GONE);
        }
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        SC sc = new SC();
                        JSONObject object = sc.storage(sp);
                        try {
                                object.put("KXA01",getIntent().getExtras().getInt("dockid"));

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        String json = object.toString();
                        String result = NetWorkUtils.getCount(baseClass.find_coor_allcustomer_URL+1,json);
                        list = CoorCustomerJson.jsonVisitMessage(result);
                        if (list!=null){
                            finish();
                        }else{
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(CustomerAddActivity.this, "无客户记录！", Toast.LENGTH_SHORT).show();
                                }
                            });
                        }

                    }
                }).start();

            }
        });
        //创建选择器
        mpv = new TimePickerView(this, TimePickerView.Type.YEAR_MONTH_DAY);
        //设置时间范围
        Calendar calendar = Calendar.getInstance();
        mpv.setRange(calendar.get(Calendar.YEAR) - 100, calendar.get(Calendar.YEAR) + 100);
        //插入时间
        mpv.setTime(new Date());
        //选择监听
        mpv.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {

            @Override
            public void onTimeSelect(Date date) {
                date1 =date;
                next_time=getTime(date);
                txt_next_time.setText(next_time);
            }
        });
        btn_show.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mpv.show();
            }
        });
        //创建选择器
        mpv1 = new TimePickerView(this, TimePickerView.Type.YEAR_MONTH_DAY);
        //设置时间范围
        Calendar calendar1 = Calendar.getInstance();
        mpv1.setRange(calendar.get(Calendar.YEAR) - 100, calendar1.get(Calendar.YEAR) + 100);
        //插入时间
        mpv1.setTime(new Date());
        //选择监听
        mpv1.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {

            @Override
            public void onTimeSelect(Date date) {
                birth_time=getTime(date);
                txt_birth_time.setText(birth_time);
            }
        });
        btn_show1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mpv1.show();
            }
        });
        showbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(CustomerAddActivity.this, ProvinceActivity.class);
                GetAreData.pname=null;
                GetAreData.aname = null;
                GetAreData.cname = null;
                GetAreData.pid = null;
                GetAreData.cid = null;
                GetAreData.aid = null;
                startActivity(intent);
            }
        });
        new Thread(new Runnable() {
            @Override
            public void run() {
                SC sc = new SC();
                JSONObject object = sc.storage(sp);
                try {
                    object.put("admin",sp.getString("admin",null));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                String json = object.toString();
                managerDao manaDao=new managerDaoImpl();
                managerList=manaDao.queryAllManager(baseClass.query_manager_url,json);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        for (int i2=0;i2<managerList.size();i2++){
                            name_list.add( managerList.get(i2).getManager_name());
                        }
                        adapter1 = new ArrayAdapter(CustomerAddActivity.this,R.layout.spinner_display_style,R.id.txtvwSpinner, name_list);
                        adapter1.setDropDownViewResource(R.layout.spinner_dropdown_style);
                        spinner_manager.setAdapter(adapter1);

                        spinner_manager.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                            @Override
                            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                                managerName=managerList.get(i).getManager_name();
                                manager_id = managerList.get(i).getManager_id();
                            }

                            @Override
                            public void onNothingSelected(AdapterView<?> adapterView) {

                            }
                        });
                        if ("updata".equals(getIntent().getStringExtra("updata"))&&c==1){
                            setMessage();
                            c++;
//                            Log.d("aa","cc");
                        }
                    }
                });
            }
        }).start();
        rg_sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if (i==rb_men.getId()){
                    sex=1;
                }else{
                    sex=2;
                }
            }
        });
        img_commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String a = getTime(new Date());
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                boolean b = ZhengZhe.idCard(edit_id_card.getText().toString());
                try {
                    date2 =  formatter.parse(a);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                if ("".equals(edit_name.getText().toString())||"".equals(edit_mobile_phone.getText().toString())||"".equals(edit_age.getText().toString())) {

                    Toast.makeText(CustomerAddActivity.this, "请填写必填项!", Toast.LENGTH_SHORT).show();
                }else if("".equals(txt_adress.getText().toString())){
                    Toast.makeText(CustomerAddActivity.this, "请选择行政区域!", Toast.LENGTH_SHORT).show();
                }else if(!ZhengZhe.phone(edit_mobile_phone.getText().toString())){
                    Toast.makeText(CustomerAddActivity.this, "请输入正确的手机号码!", Toast.LENGTH_SHORT).show();
                }else if(date1!=null&&date2.after(date1)){
                    Toast.makeText(CustomerAddActivity.this, "请选择正确的时间!", Toast.LENGTH_SHORT).show();
                }else if(!"".equals(edit_id_card.getText().toString())&&!b){
                    Toast.makeText(CustomerAddActivity.this, "请填写正确的身份证号!", Toast.LENGTH_SHORT).show();
                }else  {
                    AlertDialog.Builder builder;
                    builder = new AlertDialog.Builder(new ContextThemeWrapper(CustomerAddActivity.this, R.style.custom_dialog));
                    builder.setTitle("是否要完善客户信息？");
                    builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            if ("updata".equals(getIntent().getStringExtra("updata"))){

                            }else {
                                GetCustumer.create();
                                GetContact.create();
                            }
                            customer = GetCustumer.customers;
                            customer.setClient_gender(sex);
                            customer.setClient_name(edit_name.getText().toString());
                            if (!edit_age.getText().toString().equals("")){
                                customer.setClient_age(Integer.parseInt(edit_age.getText().toString()));
                            }
                            if ("kehuchi".equals(getIntent().getStringExtra("kehuchiTag"))) {
                                customer.setClient_Manager(0);
                            } else {
                                customer.setClient_Manager(manager_id);
                            }
                            customer.setIDNumber(edit_id_card.getText().toString());
                            customer.setClient_Address(edit_adress.getText().toString());
                            customer.setNext_Time(txt_next_time.getText().toString());
                            if (pid!=null){
                                customer.setPid(Integer.valueOf(pid));
                                customer.setCid(Integer.valueOf(cid));
                                customer.setAid(Integer.valueOf(aid));
                                customer.setPname(pname);
                                customer.setCname(cname);
                                customer.setAname(cname);
                            }
                            customer.setBirthDay(txt_birth_time.getText().toString());
                            contact = GetContact.contact;
                            contact.setContact_name(edit_contact_name.getText().toString());
                            contact.setFirst_contact_mobile_phone(edit_mobile_phone.getText().toString());
                            Intent intent = new Intent(CustomerAddActivity.this, CustomerAddContactMessageActivity.class);
                            intent.putExtra("wanshan", "wanshan");
                            intent.putExtra("dockid",getIntent().getIntExtra("dockid",0));
                            if ("updata".equals(getIntent().getStringExtra("updata"))){
                                intent.putExtra("updata","updata");

                            }
                            if ("ClueToCustomer".equals(getIntent().getStringExtra("ClueToCustomerTag"))){
                                intent.putExtra("ClueToCustomerTag","ClueToCustomer");
                            }
                            if ("ClueToCustomerGroup".equals(getIntent().getStringExtra("ClueToCustomerGroupTag"))){
                                intent.putExtra("ClueToCustomerGroupTag","ClueToCustomerGroup");
                            }
                            startActivity(intent);

                        }
                    });
                    builder.setNegativeButton("否", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            if ("ClueToCustomer".equals(getIntent().getStringExtra("ClueToCustomerTag"))||("ClueToCustomerGroup".equals(getIntent().getStringExtra("ClueToCustomerGroupTag")))){
                                clue_change();
                            }else{
                                add();

                            }
                        }
                    });
                    builder.setCancelable(false);
                    builder.show();
                }
            }
        });
    }

    private void setSFMessage() {
        PeopleEntity peo = GetPeople.getpeople();
        edit_name.setText(peo.getName());
        String bd = peo.getBirthday();
        txt_birth_time.setText(bd.substring(0,4)+"-"+bd.substring(4,6)+"-"+bd.substring(6,8));
        edit_adress.setText(peo.getAddress());
        edit_id_card.setText(peo.getIdcard());
        if ("男".equals(peo.getSex())) {
            rb_men.setChecked(true);
        } else if ("女".equals(peo.getSex())) {
            rb_women.setChecked(true);
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd");
        String s = format.format(new Date());
        edit_age.setText(Integer.valueOf(s.substring(0,4))-Integer.valueOf(bd.substring(0,4))+"");
//        Log.d("cc",Integer.valueOf(s.substring(0,4))+"-"+Integer.valueOf(bd.substring(0,4)));

    }

    public static String getTime(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }
    //线索转化为客户，以前的信息显示方法
    public void showMessage() {
        clue = GetClue.get();
        edit_name.setText(clue.getClue_name());
        if (clue.getClue_gender() != 0) {
            if (clue.getClue_gender() == 1) {
                rb_men.setChecked(true);
            } else if (clue.getClue_gender() == 2) {
                rb_women.setChecked(true);
            }
        }
        edit_age.setText(clue.getClue_age() + "");
        edit_contact_name.setText(clue.getClue_contact());
        edit_mobile_phone.setText(clue.getClue_mobile_phone());
        SpinnerAdapter spadapter4 = spinner_manager.getAdapter();
        int i4 = clue.getClue_manager();
        if (i4 != 0) {
            for (int i = 0; i < managerList.size(); i++) {
                if (i4 == managerList.get(i).getManager_id()) {
                    Log.d("aa","bb");
                    manager_name_show = managerList.get(i).getManager_name();

                }
            }
        }
        if (manager_name_show != null) {
            for (int i = 0; i < spadapter4.getCount(); i++) {
                if (manager_name_show.equals(spadapter4.getItem(i).toString())) {
                    spinner_manager.setSelection(i);
                }
            }
        }
        SpinnerAdapter spadapter5 = spinner_disease_category.getAdapter();
        Integer i5 = clue.getClue_disease_category_id();
        if (i5!=null){
            for (int i = 0;i<customerSpinner.size();i++){
                if (i5==Integer.valueOf(customerSpinner.get(i).get("BGLX01"))){
                    disease_category_show= customerSpinner.get(i).get("BGLX02");
                }
            }
        }
        if(disease_category_show!=null) {
            for (int i = 0; i < spadapter5.getCount(); i++) {
                if (disease_category_show.equals(spadapter5.getItem(i).toString())) {
                    spinner_disease_category.setSelection(i);
                }
            }
        }
        if (clue.getClue_next_time()!=null&&!clue.getClue_next_time().equals("")) {
            boolean b = clue.getClue_next_time().matches("\\d{4}-\\d{2}-\\d{2}");
            if (b) {
                txt_next_time.setText(clue.getClue_next_time());
            } else {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
                String next_time = clue.getClue_next_time();
                long l = Long.parseLong(next_time);
                Date date = new Date(l);
                String nTime = sdf.format(date);
                txt_next_time.setText(nTime);
            }
        }
    }
    public void clue_change(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                JSONObject json=new JSONObject();
                customer=GetCustumer.get();
                try {
                    json.put("KAA02",edit_name.getText().toString());
                    json.put("KAA03", edit_age.getText().toString());
                    json.put("KAA04", sex);
                    if (manager_id!=0){
                        json.put("KAA09",manager_id);
                        json.put("KAA09A",managerName);
                    }
                    json.put("AKB01",disease_category_id);
                    json.put("KAA10", txt_next_time.getText().toString());
                    json.put("KLA06",edit_mobile_phone.getText().toString());
                    json.put("KLA02",edit_contact_name.getText().toString());
                    json.put("KLA14",1);
                    clue=GetClue.get();
                    json.put("XAA01",clue.getClue_id());
                    json.put("XAA02",clue.getClue_manager());
                    json.put("XAA03",clue.getClue_name());
                    json.put("XAA04",clue.getClue_gender());
                    json.put("XAA05",clue.getClue_age());
                    json.put("AKB01",clue.getClue_disease_category_id());
                    json.put("XAA06",clue.getClue_contact());
                    json.put("XAA07",clue.getClue_phone());
                    json.put("XAA08",clue.getClue_mobile_phone());
                    json.put("XAA09",clue.getClue_mail());
                    json.put("XAA10",clue.getClue_weibo());
                    json.put("XAA11",clue.getClue_Zip_Code());
                    json.put("XAA12",clue.getClue_address());
                    json.put("AKC01",clue.getClue_channel_classification_id());
                    json.put("AKD01",clue.getClue_channel_source_id());
                    json.put("XAA13",clue.getClue_department());
                    if (clue.getClue_next_time()!=null) {
                        boolean b = clue.getClue_next_time().matches("\\d{4}-\\d{2}-\\d{2}");
                        if (b) {
                            json.put("XAA14", clue.getClue_next_time());
                        } else {
                            SimpleDateFormat sdf = new SimpleDateFormat(" yyyy-MM-dd  ");
                            String next_time = clue.getClue_next_time();
                            long l = Long.parseLong(next_time);
                            Date date = new Date(l);
                            String nTime = sdf.format(date);
                            json.put("XAA14", nTime);
                        }
                    }
                    json.put("XAA15",clue.getClue_next_content());
                    json.put("XAA16",clue.getClue_additional());
                    json.put("XAA17",clue.getClue_follow_status());
                    json.put("XAA18",clue.getClue_is_attention());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                String str=json.toString();
                String result= NetWorkUtils.getCount(baseClass.ClueToCustomer_url,str);
                try {
                    JSONObject object=new JSONObject(result);
                    final String boolen=object.getString("boolean");
                    if ("success".equals(boolen)){
                        if ("ClueToCustomerGroup".equals(getIntent().getStringExtra("ClueToCustomerGroupTag"))){
                            clueDao clueDao=new clueDaoImpl();
                            List<ClueMeesageEntity> clueMeesageEntities=clueDao.querySimpleClueMessage(baseClass.clue_group_url,null);
                            GetClueData.CreateData();
                            GetClueData.clue=clueMeesageEntities;
                            finish();
                            ClueDetailedMessageActivity.a.finish();
                        }else {
                            clueDao clueDao = new clueDaoImpl();
                            List<ClueMeesageEntity> clueMeesageEntities = clueDao.querySimpleClueMessage(baseClass.query_all_clue_message_ul, null);
                            GetClueData.CreateData();
                            GetClueData.clue = clueMeesageEntities;
                            finish();
                            ClueDetailedMessageActivity.a.finish();
                        }

                    }else{
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(CustomerAddActivity.this, boolen, Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }).start();
    }
    public void add() {
        boolean b = ZhengZhe.phone(edit_mobile_phone.getText().toString());
        if (b) {
            img_commit.setVisibility(View.GONE);
            new Thread(new Runnable() {
                @Override
                public void run() {
                    SC sc = new SC();

                    JSONObject json = sc.storage(sp);

                    try {
                        json.put("KAA02", edit_name.getText().toString());
                        json.put("KAA03", edit_age.getText().toString());
                        if (rb_men.isChecked()) {
                            json.put("KAA04", 1);
                        } else if (rb_women.isChecked()) {
                            json.put("KAA04", 2);
                        }

                        json.put("KAA07", edit_id_card.getText().toString());
                        json.put("KAA08", edit_adress.getText().toString());
                        if ("kehuchi".equals(getIntent().getStringExtra("kehuchiTag"))) {
                            json.put("KAA09", 0);
                        } else {
                            json.put("KAA09", manager_id);
                            json.put("KAA09A", managerName);
                        }
                        if ("coor".equals(getIntent().getStringExtra("TAG"))) {
                            json.put("KXA01", getIntent().getIntExtra("dockid", 0));
                        }
                        if ("updata".equals(getIntent().getStringExtra("updata"))) {
                            json.put("addoredit", "edit");
                            json.put("KAA01", customer.getClient_id());
                        } else {
                            json.put("addoredit", "add");
                        }
                        if (pid != null) {
                            json.put("KAA23", Integer.valueOf(pid));
                            json.put("KAA24", Integer.valueOf(cid));
                            json.put("KAA25", Integer.valueOf(aid));
                            json.put("KAA30", pname);
                            json.put("KAA31", cname);
                            json.put("KAA32", aname);
                        }
                        json.put("KAA05", txt_birth_time.getText().toString());
                        json.put("KAA10", txt_next_time.getText().toString());
                        json.put("KLA06", edit_mobile_phone.getText().toString());
                        json.put("CID03", sp.getString("CID03", null));
                        json.put("KLA02", edit_contact_name.getText().toString());
                        json.put("KLA14", 1);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    String sjson = json.toString();
                    //新增客户信息
                    sresult = NetWorkUtils.getCount(baseClass.add_coor_customer_URL, sjson);
                    Log.d("cc", sresult);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (sresult != null) {
                                try {
                                    JSONObject object = new JSONObject(sresult);
                                    boolean b = object.getBoolean("ifsuccess");
                                    if (b) {
                                        if ("updata".equals(getIntent().getStringExtra("updata"))) {
                                            finish();
                                        } else {
                                            customer_id = object.getInt("KAA01");
                                            AlertDialog.Builder builder1;
                                            builder1 = new AlertDialog.Builder(new ContextThemeWrapper(CustomerAddActivity.this, R.style.custom_dialog));
                                            builder1.setTitle("是否跳转预约页面？");
                                            builder1.setPositiveButton("是", new DialogInterface.OnClickListener() {
                                                @Override
                                                public void onClick(DialogInterface dialog, int which) {
                                                    Intent intent = new Intent(CustomerAddActivity.this, CustomerAppointmentAcitity.class);
                                                    intent.putExtra("ID", customer_id);
                                                    intent.putExtra("dockid",getIntent().getIntExtra("dockid", 0));
                                                    startActivity(intent);
                                                    finish();
                                                }
                                            });
                                            builder1.setNegativeButton("否", new DialogInterface.OnClickListener() {
                                                @Override
                                                public void onClick(DialogInterface dialog, int which) {
                                                    Intent intent = new Intent(CustomerAddActivity.this, CoorCustomerActivity.class);
                                                    intent.putExtra("dockid",getIntent().getIntExtra("dockid", 0));
                                                    Log.d("bb+++++++",getIntent().getIntExtra("dockid", 0)+"");
                                                    startActivity(intent);
                                                    finish();
                                                }
                                            });
                                            builder1.setCancelable(false);
                                            builder1.show();
                                        }
                                    } else {
                                        img_commit.setVisibility(View.VISIBLE);
                                        Toast.makeText(CustomerAddActivity.this, "添加客户失败", Toast.LENGTH_SHORT).show();
                                    }
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    });
                }
            }).start();


        } else {
            img_commit.setVisibility(View.VISIBLE);
            Toast.makeText(this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
        }
    }

    public void setMessage(){
        customer = GetCustumer.get();
        contact = GetContact.get();
        for (int i = 0;i<name_list.size();i++){
            if (name_list.get(i).equals(customer.getClient_manager_name())){
                spinner_manager.setSelection(i);
            }
        }

        edit_name.setText(customer.getClient_name());
        edit_id_card.setText(customer.getIDNumber());
        edit_adress.setText(customer.getClient_Address());
        edit_age.setText(customer.getClient_age()+"");
        txt_birth_time.setText(customer.getBirthDay());
        txt_next_time.setText(customer.getNext_Time());
        txt_adress.setText(customer.getPname()+customer.getCname()+customer.getAname());
        edit_contact_name.setText(contact.getContact_name());
        edit_mobile_phone.setText(contact.getFirst_contact_mobile_phone());
        if (customer.getClient_gender() != 0) {
            if (customer.getClient_gender() == 1) {
                rb_men.setChecked(true);
            } else if (customer.getClient_gender() == 2) {
                rb_women.setChecked(true);
            }
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        if (GetAreData.pname!=null){
            pid = GetAreData.pid;
            pname = GetAreData.pname;
            cid = GetAreData.cid;
            cname = GetAreData.cname;
            aid = GetAreData.aid;
            aname =GetAreData.aname;
            txt_adress.setText(pname+cname+aname);
        }
    }

}
